package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.ShowProgressDialog;
import com.luck.easyfoodbatch.mvp.mine.ModifyPasswordModel;
import com.luck.easyfoodbatch.mvp.mine.ModifyPasswordPresenter;
import com.luck.easyfoodbatch.mvp.mine.ModifyPasswordView;
import com.luck.easyfoodbatch.mvp.model.ModifyPswModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdatePasswordActivity extends BaseActivity implements ModifyPasswordView {

    @BindView(R.id.et_old_psw)
    EditText etOldPsw;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.et_psw_again)
    EditText etPswAgain;
    @BindView(R.id.btn)
    Button btn;
    private String password = "";
    private ModifyPasswordModel passwordModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password, "修改密码", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn)
    public void onClick() {
        String oldPsw = etOldPsw.getText().toString().trim();
        String psw = etPsw.getText().toString().trim();
        String pswAgain = etPswAgain.getText().toString().trim();
        if (Tools.isNull(oldPsw)) {
            T.showToast(mContext, "请输入原始密码");
        } else if (Tools.isNull(psw)) {
            T.showToast(mContext, "请输入您的新密码");
        } else if (psw.length() < 6) {
            T.showToast(mContext, "密码长度不能少于6位");
        } else if (psw.equals(oldPsw)) {
            T.showToast(mContext, "新密码不能和原始密码一样");
        } else if (Tools.isNull(pswAgain)) {
            T.showToast(mContext, "请再次输入确认密码");
        } else if (!psw.equals(pswAgain)) {
            T.showToast(mContext, "两次密码输入不一致");
        } else {
            showProgressDialog("正在修改，请稍候……");
            passwordModel = new ModifyPasswordPresenter(this, mContext);
            passwordModel.modifyPassword(oldPsw, psw);
        }
    }

    @Override
    public void modifyPasswordSuccess(ModifyPswModel model) {
        if (model.getStatus() == HttpUtil.RESULT_OK) {
            sendBroadcast("action.app.logout");
            App.getInstance().saveObject(null, HttpUtil.USER_KEY);
            startAct(LoginActivity.class);
            finish();
        } else {
            T.showToast(mContext, model.getMsg());
        }
    }

    @Override
    public void httpStatusError() {

    }


    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
